// pages/product/details/index.js
const app = getApp();
const util = require('../../utils/util.js');
let _this;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: app.globalData.xieYouUserInfo,
    indicatorDots: true,
    interval: 5000,
    autoplay: true,
    duration: 500,
    circular:true,
    nav: [{
        name: '概况'
      },
      {
        name: '详情'
      }
    ],
    current_index: "",
    detailMassage:[],
    joinShopCar: false,
    countShopCar: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    _this = this;
    console.log(options);
    _this.setData({
      id: options.productId,
      userInfo: app.globalData.xieYouUserInfo,
    })
    _this.getDetail();
    _this.getShopCar();
  },
  getDetail:function(res){
    let data={};
    data.id = _this.data.id;
    data.userCode = _this.data.userInfo.code;
    app.getMassage(data, null, 'productDetail', function(res){
      if (res.status == "success"){
        _this.setData({
          detail: res.data,
        })
      }
    }, 'GET')
  },
  getShopCar: function () {
    app.getMassage(null, _this.data.userInfo.code, 'getShopCar', function (res) {
      console.log(res);
      _this.setData({
        shopCarId: res.data.id,
      })
    }, 'GET')
  },
  /*  加减数量 */
  dataChange:function(e){
    let countShopCar = _this.data.countShopCar,
      type = e.currentTarget.dataset.type;
    if(type == 'less' && countShopCar == 0) return;
    type == 'add' ? countShopCar++ : countShopCar--;
    _this.setData({
      countShopCar: countShopCar
    })
  },
  buyNow:function(){
    var payload = _this.data.payload;
    payload.formId = e.detail.formId;
    // 1、先核算
    app.placeRepairOrder(payload, function (res) {
      if (res.data.status == 'success') {
        app.repairOrderPay(payload.id, function (res) {
          wx.requestPayment({
            'timeStamp': res.timeStamp,
            'nonceStr': res.nonceStr,
            'package': res.package,
            'signType': 'MD5',
            'paySign': res.paySign,
            'success': function (res) {
              console.log('支付成功', res)
              _this.loadReparingByUser();
            },
            'fail': function (res) {
              console.log('支付失败', res)
              _this.loadReparingByUser();
            }
          })
        });
      }
    })
  },
  addShopCar:function(){
    let data = {}, detail = _this.data.detail;
    data.product = detail;
    data.brandId = detail.brandId
    data.brandName = detail.brandName
    data.categoryCode = detail.category.code
    data.categoryName = detail.category.name
    data.count = _this.data.countShopCar;
    data.isAllPick = true
    data.leadUserCode= null
    data.modelName = detail.applyModelName
    data.price = detail.price
    data.productName = detail.name
    data.seckill = detail.seckill
    app.getMassage(data, _this.data.shopCarId + '/item/new','shopAdd',function(res){
      if (res.status == 'success'){
        wx.showToast({
          title: '已经加入购物车',
          icon: 'success',
          duration: 2000
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  select: (e) => {
    let index = e.currentTarget.dataset.index
    _this.setData({
      current_index: index
    })
  },
  // 去**页
  goNavTo: function (e) {
    app.goNavTo(e, _this.data.buttonClicked, _this);
  },
  // 返回上一页
  goBack: function (e) {
    app.goBack(e, _this.data.buttonClicked, _this);
  },
  // 开关
  toggleSwitch:function(e){
    app.toggleSwitch(e,_this);
  }
})